var PersonModel = require("../model/PersonModel");

const PersonService = {
  addPerson: (person, avatar) => {
    return PersonModel.create({ ...person, avatar });
  },
  updatePerson: (_id, person) => {
    return PersonModel.updateOne({ _id }, person);
  },
  getPersonOrList: (_id, page, limit) => {
    return _id
      ? PersonModel.find({ _id }, ["username", "age", "avatar"])
      : PersonModel.find({}, ["username", "age", "avatar"])
          .sort({ age: -1 })
          .skip((page - 1) * limit)
          .limit(limit);
    // return PersonModel.find({_id});   // 失败
  },
  delPerson: (_id) => {
    return PersonModel.deleteOne({ _id });
  },
  login: (person) => {
    return PersonModel.findOne(person);
  },
};

module.exports = PersonService;
